var xmlhttp;
var username;

function mintajax() {
	var xmlHttpObj;
	if (window.XMLHttpRequest) {
		xmlHttpObj = new XMLHttpRequest( );
	} else {
		try {
		xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlHttpObj = false;
			}
		}
	}
	return xmlHttpObj;
}

function printResponse(){
	if (xmlhttp.readyState== 4 && xmlhttp.status== 200){
		alert(xmlhttp.getAllResponseHeaders());
		alert(xmlhttp.responseText);
	}
}

function getResponse(url,qry,tipe,fungsi){
	//url untuk get, harus masukin parameter dibelakang '?'
	//tipe 1=get, 2=post
	//fungsi berupa parameter data
	
	if (!xmlhttp)
		xmlhttp = ajaxing();
	if (!xmlhttp)
		return;
	
	if (tipe==1){
		xmlhttp.open('GET', url, true);
		xmlhttp.onreadystatechange = fungsi;
		xmlhttp.send(null);
	}else{
		xmlhttp.open('POST', url, true);
		xmlhttp.onreadystatechange = fungsi;
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(qry);
	}
}

function getValue(varname) {
	var url = window.location.href;
	var qparts = url.split("?");
	if (qparts.length == 0) {
		return "";
	}
	var query = qparts[1];
	var vars = query.split("&");
	var value = "";
	for (i=0;i<vars.length;i++) {
		var parts = vars[i].split("=");
		if (parts[0] == varname) {
			value = parts[1];
			break;
		}
	}
	value = unescape(value);
	value = value.replace(/\+/g," ");
	return value;
}

function trim(s){
  return s.replace(/^\s+|\s+$/g, "");
}

function getXmlHttpObj() {
	if (window.XMLHttpRequest){	// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else { // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}

function moveLogin() {
	window.location = 'profile.php?uid=' + username;
}

function login() {
	if ((document.getElementById("username").value == "") || (document.getElementById("passwd").value == "")) {
		document.getElementById("loginResultDiv").innerHTML = "Please fill in the blank field";
		return;
	}
	
	xmlhttp = getXmlHttpObj();
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("loginResultDiv").innerHTML = xmlhttp.responseText;
			if (xmlhttp.responseText.substring(0, 7) == "Welcome") {
				username = xmlhttp.responseText.substring(9);
				timer=setTimeout('moveLogin()', 1000);
			}
		}
	}
	xmlhttp.open("GET", "prologin.php?username="+document.getElementById("username").value+"&passwd="+document.getElementById("passwd").value, true);
	xmlhttp.send();
}

function search() {
	if (document.getElementById("searchbar").value != "") {
		window.location = 'search.php?q=' + document.getElementById("searchbar").value + '&o=' + document.getElementById("searchopt").value;
	}
}
